package org.linlinjava.litemall.admin.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallAd;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.domain.LitemallWxExpress;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.db.service.LitemallWxExpressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/express")
@Validated
public class AdminWxExpressController {
    private final Log logger = LogFactory.getLog(AdminWxExpressController.class);

    @Autowired
    private LitemallWxExpressService wxExpressService;

    @RequiresPermissions("admin:wx:express")
    @RequiresPermissionsDesc(menu = {"订单管理", "物流助手"}, button = "查询")
    @GetMapping("/list")
    public Object list(String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<LitemallWxExpress> wxExpressList = wxExpressService.querySelective(name, page, limit, sort, order);
        return ResponseUtil.okList(wxExpressList);
    }

    @RequiresPermissions("admin:wx:express")
    @RequiresPermissionsDesc(menu = {"订单管理", "物流助手"}, button = "查询")
    @GetMapping("/all")
    public Object all() {
        List<LitemallWxExpress> list = wxExpressService.queryList();
        return ResponseUtil.ok(list);
    }

    private Object validate(LitemallWxExpress wxExpress) {
        String name = wxExpress.getName();
        if (StringUtils.isEmpty(name)) {
            return ResponseUtil.badArgument();
        }

        Short type = wxExpress.getType();
        if (type == null) {
            return ResponseUtil.badArgument();
        }

        String deliveryId = wxExpress.getDeliveryId();
        if (StringUtils.isEmpty(deliveryId)) {
            return ResponseUtil.badArgument();
        }

        String serviceType = wxExpress.getServiceType();
        if (StringUtils.isEmpty(serviceType)) {
            return ResponseUtil.badArgument();
        }

        String bizId = wxExpress.getBizId();
        if (StringUtils.isEmpty(bizId)) {
            return ResponseUtil.badArgument();
        }

        return null;

    }

    @RequiresPermissions("admin:wx:express")
    @RequiresPermissionsDesc(menu = {"订单管理", "物流助手"}, button = "查询")
    @PostMapping("/update")
    public Object update(@RequestBody LitemallWxExpress wxExpress) {
        Object error = validate(wxExpress);
        if (error != null) {
            return error;
        }
        wxExpressService.updateById(wxExpress);
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:wx:express")
    @RequiresPermissionsDesc(menu = {"订单管理", "物流助手"}, button = "查询")
    @PostMapping("/delete")
    public Object delete(@RequestBody LitemallWxExpress wxExpress) {
        Integer id = wxExpress.getId();
        if (id == null) {
            return ResponseUtil.badArgument();
        }
        wxExpressService.deleteById(id);
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:wx:express")
    @RequiresPermissionsDesc(menu = {"订单管理", "物流助手"}, button = "查询")
    @PostMapping("/create")
    public Object create(@RequestBody LitemallWxExpress wxExpress) {
        Object error = validate(wxExpress);
        if (error != null) {
            return error;
        }
        wxExpressService.add(wxExpress);
        return ResponseUtil.ok(wxExpress);
    }


}
